package com.gfxy.modules.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gfxy.modules.common.entity.CurrentDate;
import com.gfxy.modules.common.entity.TotalScore;
import com.gfxy.modules.common.mapper.ScoreUpdateMapper;
import com.gfxy.modules.common.service.ITotalScoreService;
import com.gfxy.modules.common.service.impl.CurrentDateService;
import com.gfxy.modules.manage.entity.AssessmentProjectsClassification;
import com.gfxy.modules.manage.entity.AssessmentProjectsItems;
import com.gfxy.modules.manage.entity.AssessmentProjectsItemsVo;
import com.gfxy.modules.manage.mapper.AssessmentProjectsItemsMapper;
import com.gfxy.modules.manage.service.IAssessmentProjectsItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 考核项
 * @Author: jeecg-boot
 * @Date:   2023-09-01
 * @Version: V1.0
 */
@Service
public class AssessmentProjectsItemsServiceImpl extends ServiceImpl<AssessmentProjectsItemsMapper, AssessmentProjectsItems> implements IAssessmentProjectsItemsService {

    @Autowired
    private AssessmentProjectsItemsMapper assessmentProjectsItemsMapper;
    @Autowired
    public CurrentDateService currentDateService;
    @Autowired
    ScoreUpdateMapper scoreUpdateMapper;

    @Autowired
    ITotalScoreService totalScoreService;


    @Override
    public List<Map<String, String>> querySurplusItemByProjectId(String projectId) {
        return assessmentProjectsItemsMapper.querySurplusItemByProjectId(projectId);
    }

    @Override
    public List<AssessmentProjectsItemsVo> queryByProjectIdAndClassificationId(String projectId, String classificationId) {
        return assessmentProjectsItemsMapper.queryByProjectIdAndClassificationId(projectId,classificationId);
    }

    public void updateTotalScore(AssessmentProjectsItems assessmentProjectsItems){
        CurrentDate currentDate  = currentDateService.getCurrentDate();

        QueryWrapper<TotalScore> totalScoreQW = new QueryWrapper<TotalScore>()
                .eq("date", currentDate.getCurrentDate())
                .eq("classification_id",assessmentProjectsItems.getClassificationId());
        totalScoreService.remove(totalScoreQW);



        List<Map> scores = scoreUpdateMapper.getTotalScoreByClassificationId(currentDate.getCurrentDate(),assessmentProjectsItems.getClassificationId());
        List<TotalScore> totalScores = new ArrayList<>();
        for (Map score : scores) {
            TotalScore totalScore = new TotalScore();
            totalScore.setScore(Double.parseDouble(score.get("score").toString()));
            totalScore.setClassificationId(assessmentProjectsItems.getClassificationId());
            totalScore.setProjectId(Integer.parseInt(assessmentProjectsItems.getProjectId()));
            totalScore.setOrgCode(String.valueOf(score.get("org_code")));
            totalScore.setTeacherId(String.valueOf(score.get("teacher_id")));
            totalScore.setDate(currentDate.getCurrentDate());
            totalScores.add(totalScore);
        }
        totalScoreService.saveBatch(totalScores);
    }

}
